package com.jh3502.vivo.function.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import com.jh3502.vivo.R;
import com.jh3502.vivo.base.BaseActivity;
import com.jh3502.vivo.function.reimnew.ReimNewPresenter;

import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author harisucici
 * @version 2018/12/11 10:10
 * name
 * todo
 * @email harisucici@gmail.com
 */
public class TravelPersonChooseDialog extends BaseActivity<ReimNewPresenter.ReimNewView, ReimNewPresenter> implements ReimNewPresenter.ReimNewView {

    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.list)
    ListView list;
    private String personName;
    private int personId;
    private int deptId;
    private int level;
    private int postId;
    private double standard;


    @Override
    public ReimNewPresenter initPresenter() {
        return new ReimNewPresenter();
    }

    @Override
    protected int setViewId() {
        return R.layout.activity_travel_person_choose;
    }

    @Override
    protected void initView() {
        mPresenter.setReimNewView(this);
    }

    @Override
    protected void initData() {
        mPresenter.findExpensePerson();
    }

    @Override
    protected void doOnDestroy() {

    }

    @Override
    protected void doOnResume() {

    }

    @Override
    protected void doOnSaveInstanceState(Bundle savedInstanceState) {

    }

    @Override
    public void responseError(int code, String msg) {

    }


    @Override
    public void findExpensePersonSuccess(List<TravelPersonChooseBean> datas) {
        ListChoiceAdapter adapter = new ListChoiceAdapter(this, datas);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {


            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                personName = datas.get(position).getName();
                personId = datas.get(position).getUserId();
                deptId = datas.get(position).getDeptId();
                level = datas.get(position).getLevel();
                postId = datas.get(position).getPostId();
                postId = datas.get(position).getPostId();
                standard = datas.get(position).getStandard();
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void findTravelFormSuccess(List<TravelNumChooseBean> travelNumChooseBeans) {

    }

    @Override
    public void findaccountSuccess(Map<String, Object> map) {

    }

    @Override
    public void submitExpenseSuccess() {

    }

    @Override
    public void findHotelStandard(double v) {

    }


    @OnClick(R.id.cancel)
    public void onCancelClicked() {
        onBackPressed();
    }

    @OnClick(R.id.submit)
    public void onSubmitClicked() {
        if (TextUtils.isEmpty(personName)) {
            showShortToast("最少选择一项");
            return;
        }
        Intent intent = new Intent();
        intent.putExtra("personName", personName);
        intent.putExtra("personId", personId);
        intent.putExtra("deptId", deptId);
        intent.putExtra("level", level);
        intent.putExtra("postId", postId);
        intent.putExtra("standard", standard);
        setResult(Activity.RESULT_OK, intent);
        onBackPressed();
    }

    private class ListChoiceAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        private List<TravelPersonChooseBean> list;

        public ListChoiceAdapter(Context context, List<TravelPersonChooseBean> list) {
            this.inflater = LayoutInflater.from(context);
            this.list = list;


        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.item_list_choice, null);
                holder = new ViewHolder();
                holder.item_list_choice_check = convertView.findViewById(R.id.item_list_choice_check);
                holder.item_list_choice_name = convertView.findViewById(R.id.item_list_choice_name);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            boolean check = ((ListView) parent).isItemChecked(position);
            holder.item_list_choice_check.setChecked(check);
            holder.item_list_choice_name.setText(list.get(position).getName());


            return convertView;
        }

        private class ViewHolder {
            CheckBox item_list_choice_check;
            TextView item_list_choice_name;

        }
    }
}
